package cn.lizemin.intercetors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lzm
 * @version 1.0
 * @description: 自定义拦截器1
 * @date 2022/1/5 20:40
 */
public class MyInterceptor1 implements HandlerInterceptor {

    private static final Logger log = LoggerFactory.getLogger(MyInterceptor1.class);

    /**
     * controller前执行
     * @param request
     * @param response
     * @param handler 当前请求请求的控制器方法对象  DemoController#demo
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("============1=============");
        log.debug("handler: {}",handler);
        //放行
//        response.sendRedirect(request.getContextPath() + "/403.jsp");
        return true;
//        return false;
    }

    /**
     * controller后执行
     * @param request
     * @param response
     * @param handler 当前请求请求的控制器方法对象  DemoController#demo
     * @param modelAndView modelAndView 模型和视图  当前请求访问方法的modelandview对象
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.debug("============2============");
        log.debug("view:{}",modelAndView.getViewName());
    }

    /**
     * 响应后执行
     * @param request
     * @param response
     * @param handler 当前请求请求的控制器方法对象  DemoController#demo
     * @param ex 控制器出现异常时的异常对象
     * @throws Exception
     * 这个方法: finally{}代码块  总是执行  无论请求正确,还是出现异常
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.debug("============3============");
    }
}
